﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DDE_Server.Object;
using DDE_Server.Common;
using DDE_Server.Presenter;

namespace DDE_Server.View
{
    public partial class StrategyView : UserControl
    {
        private List<DataEntry> listData =  null;
        //private int currentRow = -1;
        private BindingSource bindingSource = null;
        private StrategyPresenter presenter = null;

        
        public StrategyView()
        {
            listData = new List<DataEntry>();
            InitializeComponent();
            presenter = new StrategyPresenter(this);
            bindingSource = new BindingSource();
            bindingSource.DataSource = listData;
            entryGridView.DataSource = bindingSource;
           
        }

        public StrategyPresenter Presenter {
            get {
                return presenter;
            }
        }

        public List<DataEntry> ListData {
            get {
                return listData;
            }
        }

        public override void Refresh() {
            if (InvokeRequired)
            {
                Action callback = new Action(Refresh);
                Invoke(callback);
            }
            else
            {
            entryGridView.Refresh();
        }

        }

        public void ResetBinding()
        {
            if (InvokeRequired)
            {
                Action callback = new Action(ResetBinding);
                Invoke(callback);
            }
            else
            {
            bindingSource.ResetBindings(false);
            }
        }

        private void add_Btn_Group_Click(object sender, EventArgs e)
        {
            StrategyBuilderForm mForm = new StrategyBuilderForm();
            if (mForm.ShowDialog(this) == DialogResult.OK) {
                presenter.AddRow(mForm.Entry);      
    
            }
        }

        private void entryGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
			//DataGridView gridView = sender as DataGridView;
			////if (e.RowIndex > -1) 
			////{
			////    gridView
			////}
			//if (currentRow != e.RowIndex)
			//{
			//	if (e.RowIndex > -1)
			//	{
			//		if (currentRow > -1)
			//		{
			//			//gridView.Rows[currentRow].Selected = !gridView.Rows[currentRow].Selected;
			//		}
			//		else
			//		{
			//			currentRow = e.RowIndex;
			//			//gridView.Rows[currentRow].Selected = !gridView.Rows[currentRow].Selected;
			//		}
			//	}
			//}
        }

        private void remove_btn_Click(object sender, EventArgs e)
        {
            //DataGridView gridView = sender as DataGridView;
			
			
			for (int i = 0; i < entryGridView.SelectedRows.Count; i++) {
				int currentRow = entryGridView.SelectedRows[i].Index;
				if (currentRow > -1) {
					if (currentRow < entryGridView.Rows.Count) {
						presenter.RemoveRow(currentRow);
						currentRow = -1;
					}
				}
			}
			bindingSource.ResetBindings(false);
        }

        private void active_btn_Click(object sender, EventArgs e)
        {
			for (int i = 0; i < entryGridView.SelectedRows.Count; i++) {
				int currentRow = entryGridView.SelectedRows[i].Index;
				presenter.ResetTimer(currentRow);
			}
        }

        private void stop_btn_Click(object sender, EventArgs e)
        {
			for (int i = 0; i < entryGridView.SelectedRows.Count; i++) {
				int currentRow = entryGridView.SelectedRows[i].Index;
				presenter.StopRow(currentRow);
			}
        }



    }
}
